package com.dpl.yyrj.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import com.dpl.yyrj.R;

import java.io.File;
import java.util.Objects;

public class ReceiveImageActivity extends AppCompatActivity implements View.OnClickListener {


    private Button btn_tore;
    private final String dirPath = "12530/download/吉泽明步-1/ONSD-518 男を骨抜きにする背面騎乗/ONSD-518 男を骨抜きにする背面騎乗ファック4時間 吉沢明歩 二階堂ソフィア 沖田杏梨 麻美ゆま 青山ローラ かすみりさ Rio 小川あさ美 穂花 あいだゆあ.jpg";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive_image);


        Intent intent = getIntent();
        String action = intent.getAction();
        String type = intent.getType();

        btn_tore = findViewById(R.id.btn_goto_content);
        btn_tore.setOnClickListener(this);
        if (Intent.ACTION_SEND.equals(action) && type != null) {
            if (type.startsWith("image/")) {
            }
        }
        if (Intent.ACTION_VIEW.equals(action) && type != null) {
            if (type.startsWith("image/")) {
            }
        }
    }

    private void handleSendImage(Intent intent) {
        Uri imageUri = intent.getData();
        getPathFromUri(this, imageUri);
        if (imageUri != null) {
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
            } catch (Exception e) {
                e.printStackTrace();
                // Handle the exception
            }
        }
    }

    public String getPathFromUri(Context context, Uri uri) {
        // 尝试从 MediaStore 获取路径
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
        if (cursor != null) {
            int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            if (cursor.moveToFirst()) {
                String path = cursor.getString(columnIndex);
                cursor.close();
                return path;
            }
            cursor.close();
        }
        // 如果从 MediaStore 获取路径失败，你可以考虑其他方法，如复制文件到临时目录
        // ...
        return null; // 如果没有找到路径，返回 null
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_goto_content:
                toReview(dirPath);
                break;
        }
    }

    private void toReview(String dirPath) {

        File externalStorageDirectory = Environment.getExternalStorageDirectory();
        File file = new File(getSDRootPath(this), dirPath);
        if (!file.exists())
            return;

        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri uri = Uri.fromFile(file); // 注意：这里需要是文件URI，但Android Q+有限制
//        Uri dpl = FileProvider.getUriForFile(this, "dpl", file);
//        ImagePreviewActivity.start(this, dpl);
        intent.setDataAndType(uri, "resource/folder"); // 这个 MIME 类型可能不是所有文件管理器都支持
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivity(intent);
    }



    // 获得SD外部存储根目录一般为：/storage/emulated/0
    public static File getSDRootPath(Context context) {
        File sdPath;
        boolean isSDExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); //SD卡是否存在
        if (isSDExist) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                File externalFileRootDir = context.getExternalFilesDir("");
                do {
                    externalFileRootDir = Objects.requireNonNull(externalFileRootDir).getParentFile();
                } while (Objects.requireNonNull(externalFileRootDir).getAbsolutePath().contains("/Android"));
                sdPath = Objects.requireNonNull(externalFileRootDir);
            } else {
                sdPath = context.getExternalFilesDir("");
            }
        } else {
            sdPath = Environment.getRootDirectory();//根目录
        }

        return sdPath;
    }


}